package com.hfzy.ihk.common.core.freeswitch.robot;

import org.freeswitch.DTMFCallback;
import org.freeswitch.FreeswitchScript;
import org.freeswitch.HangupHook;
import org.freeswitch.StateHandler;
import org.freeswitch.swig.JavaSession;
import org.freeswitch.swig.freeswitch;

/**
 * Created by Administrator on 2018/10/19.
 *
 * @author wws
 */
public class Robot implements FreeswitchScript, DTMFCallback, HangupHook,StateHandler.OnInitHandler,
        StateHandler.OnDestroyHandler,StateHandler.OnConsumeMediaHandler,StateHandler.OnExecuteHandler,StateHandler.OnHangupHandler,
        StateHandler.OnRoutingHandler,StateHandler.OnSoftExecuteHandler,StateHandler.OnExchangeMediaHandler
{
    public Robot()
    {
    }

    @Override
    public String onDTMF(Object object, int i, String arg)
    {
        if (object instanceof String) {
            freeswitch.console_log("notice", "Robot DTMF: " + (String) object + " ARG: " + arg + "\n");
        } else {
            freeswitch.console_log("notice", "Robot WOW GOT AN EVENT: " + object.toString());
        }
        return "true";
    }

    @Override
    public void onHangup()
    {
        freeswitch.console_log("notice", "Robot wws mod java test--->HANGUP!\n");
    }

    @Override
    public int onConsumeMedia(String s) {
        freeswitch.console_log("notice", "Robot wws mod java test--->onConsumeMedia: " + s +"\n");
        return 0;
    }

    @Override
    public int onDestroy(String s) {
        freeswitch.console_log("notice", "Robot wws mod java test--->onDestroy: " + s +"\n");
        return 0;
    }

    @Override
    public int onExchangeMedia(String s) {
        freeswitch.console_log("notice", "Robot wws mod java test--->onExchangeMedia: " + s +"\n");
        return 0;
    }

    @Override
    public int onExecute(String s) {
        freeswitch.console_log("notice", "Robot wws mod java test--->onExecute: " + s +"\n");
        return 0;
    }

    @Override
    public int onHangup(String s, String s1) {
        freeswitch.console_log("notice", "Robot wws mod java test--->onHangup: " + s +"\n");
        return 0;
    }

    @Override
    public int onInit(String s) {
        freeswitch.console_log("notice", "Robot wws mod java test--->onInit: " + s +"\n");
        return 0;
    }

    @Override
    public int onRouting(String s) {
        freeswitch.console_log("notice", "Robot wws mod java test--->onRouting: " + s +"\n");
        return 0;
    }

    @Override
    public int onSoftExecute(String s) {
        freeswitch.console_log("notice", "Robot wws mod java test--->onSoftExecute: " + s +"\n");
        return 0;
    }

    /**<action application="export" data="dialed_extension=$1"/>
    <!-- bind_meta_app can have these args <key> [a|b|ab] [a|b|o|s] <app> -->
    <action application="bind_meta_app" data="1 b s execute_extension::dx XML features"/>
    <action application="bind_meta_app" data="2 b s record_session::$${recordings_dir}/${caller_id_number}.${strftime(%Y-%m-%d-%H-%M-%S)}.wav"/>
    <action application="bind_meta_app" data="3 b s execute_extension::cf XML features"/>
    <action application="bind_meta_app" data="4 b s execute_extension::att_xfer XML features"/>
    <action application="set" data="ringback=${us-ring}"/>
    <action application="set" data="transfer_ringback=$${hold_music}"/>
    <action application="set" data="call_timeout=30"/>
    <!-- <action application="set" data="sip_exclude_contact=${network_addr}"/> -->
    <action application="set" data="hangup_after_bridge=true"/>
    <!--<action application="set" data="continue_on_fail=NORMAL_TEMPORARY_FAILURE,USER_BUSY,NO_ANSWER,TIMEOUT,NO_ROUTE_DESTINATION"/> -->
    <action application="set" data="continue_on_fail=true"/>
    <action application="hash" data="insert/${domain_name}-call_return/${dialed_extension}/${caller_id_number}"/>
    <action application="hash" data="insert/${domain_name}-last_dial_ext/${dialed_extension}/${uuid}"/>
    <action application="set" data="called_party_callgroup=${user_data(${dialed_extension}@${domain_name} var callgroup)}"/>
    <action application="hash" data="insert/${domain_name}-last_dial_ext/${called_party_callgroup}/${uuid}"/>
    <action application="hash" data="insert/${domain_name}-last_dial_ext/global/${uuid}"/>
    <!--<action application="export" data="nolocal:rtp_secure_media=${user_data(${dialed_extension}@${domain_name} var rtp_secure_media)}"/>-->
    <action application="hash" data="insert/${domain_name}-last_dial/${called_party_callgroup}/${uuid}"/>
    <aciton application="set" data="divert_events=on"/>
    <action application="bridge" data="user/${dialed_extension}@${domain_name}"/>
    <action application="answer"/>
    <action application="sleep" data="1000"/>
    <action application="bridge" data="loopback/app=voicemail:default ${domain_name} ${dialed_extension}"/>*/
    @Override
    public void run(String sessionUuid, String args)
    {
        freeswitch.console_log("notice", "Robot UUID: " + sessionUuid + " ARGS: " + args + "\n");
        JavaSession session = null;
        try{
            session = new JavaSession(sessionUuid);
            session.execute("export","dialed_extension=$1");
            session.execute("bind_meta_app","1 b s execute_extension::dx XML features");
            session.execute("bind_meta_app","2 b s record_session::$${recordings_dir}/${caller_id_number}.${strftime(%Y-%m-%d-%H-%M-%S)}.wav");
            session.execute("bind_meta_app","3 b s execute_extension::cf XML features");
            session.execute("bind_meta_app","4 b s execute_extension::att_xfer XML features");
            session.execute("set","ringback=${us-ring}");
            session.execute("set","transfer_ringback=$${hold_music}");
            session.execute("set","call_timeout=30");
            session.execute("set","hangup_after_bridge=true");
            session.execute("set","continue_on_fail=true");
            session.execute("hash","insert/${domain_name}-call_return/${dialed_extension}/${caller_id_number}");
            session.execute("hash","insert/${domain_name}-last_dial_ext/${dialed_extension}/${uuid}");
            session.execute("set","called_party_callgroup=${user_data(${dialed_extension}@${domain_name} var callgroup)}");
            session.execute("hash","insert/${domain_name}-last_dial_ext/${called_party_callgroup}/${uuid}");
            session.execute("hash","insert/${domain_name}-last_dial_ext/global/${uuid}");
            session.execute("hash","insert/${domain_name}-last_dial/${called_party_callgroup}/${uuid}");
            session.execute("set","divert_events=on");
            session.execute("bridge","user/${dialed_extension}@${domain_name}");
            session.execute("answer",null);
            session.execute("sleep","1000");
            session.execute("bridge","loopback/app=voicemail:default ${domain_name} ${dialed_extension}");
        }finally{
            if (session != null) {
                session.delete();
            }
        }
    }

}